﻿using System.Collections.Generic;
using studentSubsidy.Dao;
using studentSubsidy.Entity;
using studentSubsidy.Entity.AdminProject;
using studentSubsidy.Entity.Apply;
using studentSubsidy.Entity.User;
using studentSubsidy.Models;
using Ubiety.Dns.Core.Records;

namespace studentSubsidy.Service
{
    public class AdminApplyService
    {
        private AdminApplyDao _adminApplyDao = new AdminApplyDao();
        private ApplyDao _applyDao = new ApplyDao();

        public Result checkSuccess(int apply_id, string role)
        {
            Apply apply = _applyDao.getApplyByApplyId(apply_id);
            Project project = _applyDao.getProjectById(apply.project_id);

            if (role == "school")
            {
                if (project.category_id == 1 || project.category_id == 3)
                {
                    _applyDao.updateCheckProgress(apply_id, 3, 1, -1);
                }
                else
                {
                    _applyDao.updateCheckProgress(apply_id, 2, 0, -1);
                }
            }

            if (role == "bureau")
            {
                _applyDao.updateCheckProgress(apply_id, 4, 1, 1);
            }


            return new Result(200, "审核完成", null, null);
        }


        public Result getAllApplyCheck(string role)
        {
            List<Apply> applies = _adminApplyDao.getAllApplyCheck(role);
            List<AllApplyReturn> allApplyReturns = new List<AllApplyReturn>();

            for (int i = 0; i < applies.Count; i++)
            {
                User user = _applyDao.getUser(applies[i].user_id);
                var applyReturn = new AllApplyReturn
                {
                    applyId = applies[i].apply_id,
                    applyTime = applies[i].apply_time.Replace("T"," "),
                    projectName = _applyDao.getProjectById(applies[i].project_id).product_name,
                    address = user.address,
                    name = user.name,
                    major = user.major,
                    phone = user.phone
                };
                allApplyReturns.Add(applyReturn);
            }

            return new Result(200, "搜索成功", allApplyReturns, null);
        }

        public Result rejectApply(RejectQuery rejectQuery)
        {
            Apply rejectApply = _applyDao.getApplyByApplyId(rejectQuery.applyId);
            if (rejectQuery.role == "school")
            {
                if (rejectApply.isCityCheck == -1)
                {
                    Apply apply = new Apply
                    {
                        apply_id = rejectApply.apply_id,
                        apply_time = rejectApply.apply_time,
                        bank_number = rejectApply.bank_number,
                        check_progress = 3,
                        project_id = rejectApply.project_id,
                        user_id = rejectApply.user_id,
                        isCheck = -1,
                        fail_remark = rejectQuery.failRemark,
                        isCityCheck = -1
                    };
                    _adminApplyDao.updateApply(apply);
                    return new Result(200, "驳回成功", null, null);
                }
                else
                {
                    Apply apply = new Apply
                    {
                        apply_id = rejectApply.apply_id,
                        apply_time = rejectApply.apply_time,
                        bank_number = rejectApply.bank_number,
                        check_progress = 2,
                        project_id = rejectApply.project_id,
                        user_id = rejectApply.user_id,
                        isCheck = -1,
                        fail_remark = rejectQuery.failRemark,
                        isCityCheck = -1
                    };
                    _adminApplyDao.updateApply(apply);
                    return new Result(200, "驳回成功", null, null);
                }
            }

            if (rejectQuery.role == "bureau")
            {
                Apply apply = new Apply
                {
                    apply_id = rejectApply.apply_id,
                    check_progress = 4,
                    isCheck = -1,
                    isCityCheck = 1,
                    fail_remark = rejectQuery.failRemark,
                    project_id = rejectApply.project_id,
                    user_id = rejectApply.user_id,
                    apply_time = rejectApply.apply_time,
                    bank_number = rejectApply.bank_number
                };
                _adminApplyDao.updateApply(apply);
                return new Result(200, "驳回成功", null, null);
            }

            return new Result(400, "驳回失败", null, null);
        }

        public Result getAllRejectApply(string role)
        {
            List<Apply> rejectApply = _adminApplyDao.getAllReject(role);
            List<AllApplyReturn> allApplyReturns = new List<AllApplyReturn>();

            for (int i = 0; i < rejectApply.Count; i++)
            {
                User user = _applyDao.getUser(rejectApply[i].user_id);
                var applyReturn = new AllApplyReturn
                {
                    applyId = rejectApply[i].apply_id,
                    applyTime = rejectApply[i].apply_time.Replace("T"," "),
                    projectName = _applyDao.getProjectById(rejectApply[i].project_id).product_name,
                    address = user.address,
                    name = user.name,
                    major = user.major,
                    phone = user.phone
                };
                allApplyReturns.Add(applyReturn);
            }

            return new Result(200, "搜索成功", allApplyReturns, null);
        }
    }
}